package v1

import (
	"ginblog/model"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 新增评论
func AddComment(c *gin.Context)  {
	// 引入结构体
	var data model.Comment
	_ = c.ShouldBindJSON(&data)
	uid,_ := c.Get("userid")
	data.Uid = uid.(int)

	// 判断文章是否存在
	_,code := model.GetArtInfo(data.Aid)
	if code != errmsg.SUCCSE{
		c.JSON(http.StatusOK, gin.H{
			"status":code,
			"message":errmsg.GetErrorMsg(code),
		})
		return
	}

	code = model.CreateComment(&data)
	model.IncrCommentArt(data.Aid)
	c.JSON(http.StatusOK, gin.H{
		"status":code,
		"message":errmsg.GetErrorMsg(code),
	})
}

// 文章的评论列表
func GetCommentList(c *gin.Context)  {
	id,_ := strconv.Atoi(c.Param("id"))
	//uid,_ := c.Get("userid")
	pageSize,_ := strconv.Atoi(c.Query("pagesize"))
	pageNum,_ := strconv.Atoi(c.Query("pagenum"))
	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = -1
	}

	// 查询文章下面的所有的评论
	data,code := model.GetCommentList(id, pageSize, pageNum)
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":data,
		"message":errmsg.GetErrorMsg(code),
	})
}
